home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 06 - 1990 / 06.05 May 90 / PrintWindow Code / MyWindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-02  |  13.2 KB  |  508 lines  |  [TEXT/KAHL]

  1. /************************************
  2. File: MyWindow.c
  3. *************************************/
  4. /**********************
  5. Include files
  6. ***********************/
  7. #include "MyWindow.h"
  8. #include "String.h"
  9.  
  10. /**********************
  11. Constants
  12. ***********************/
  13. #define    TRUE    1
  14. #define    FALSE    0
  15. #define    NIL        0
  16.  
  17. #define    I_A_Button    15
  18. #define    I_A_Checkbox    21
  19. #define    I_Radio_3    20
  20. #define    I_Radio_2    19
  21. #define    I_Radio_1    16
  22. #define    I_Scroll_bar    11
  23. #define    PictureID    14
  24. #define    PopUpID    51
  25.  
  26. /**********************
  27. Globals
  28. ***********************/
  29. extern char forPrinting;
  30.  
  31. WindowPtr    MyWindow;
  32. static ControlHandle    ScrollVHandle;
  33. static ControlHandle    C_A_Button;
  34. static ControlHandle    C_A_Checkbox;
  35. static ControlHandle    R1Control[4];
  36. static char    ChangedSelection;
  37. static TEHandle   theTE;
  38. static ListHandle  List_I_AList;
  39. static Rect    Rect_I_AList;
  40. static short    Popup_I_PopUp;
  41.  
  42. /**********************
  43. Init_MyWindow()
  44. ***********************/
  45. void  Init_MyWindow()
  46. { /* Init_MyWindow() */
  47.     MyWindow = NIL;
  48. } /* Init_MyWindow() */
  49.  
  50. /**********************
  51. Close_MyWindow() closes window
  52. ***********************/
  53. void  Close_MyWindow(whichWindow,theInput)
  54. WindowPtr  whichWindow;
  55. TEHandle *theInput;
  56. { /* Close_MyWindow() */
  57. if ((MyWindow != NIL) && 
  58.         ((MyWindow == whichWindow) || (whichWindow == (WindowPtr)-1))) {
  59.     
  60.     /* dispose of TE record and window */
  61.     if (*theInput == theTE)
  62.         *theInput = NIL;
  63.     DisposeWindow(MyWindow);
  64.     MyWindow = NIL;
  65.     }
  66. } /* Close_MyWindow() */
  67.  
  68. /**********************
  69. Resized_MyWindow() handles re-sizing of window
  70. ***********************/
  71. void Resized_MyWindow(OldRect,whichWindow)
  72. Rect    *OldRect; 
  73. WindowPtr    whichWindow; 
  74. { /* Resized_MyWindow() */
  75. WindowPtr    SavePort;
  76. Rect    tempRect, temp2Rect;
  77. short    Index;
  78.  
  79. if (MyWindow == whichWindow) { /* MyWindow */
  80.     GetPort(&SavePort);
  81.     SetPort(MyWindow);
  82.     
  83.     if (ScrollVHandle != NIL) { /* take care of scrollbar */
  84.         HLock((Handle)ScrollVHandle);
  85.         
  86.         /* Invalidate old rectangle of scrollbar */
  87.         tempRect.left = (*ScrollVHandle)->contrlRect.left-4;
  88.         tempRect.top = (*ScrollVHandle)->contrlRect.top;
  89.         tempRect.right = (*ScrollVHandle)->contrlRect.right;
  90.         tempRect.bottom = (*ScrollVHandle)->contrlRect.bottom+16;
  91.         InvalRect(&tempRect);
  92.         
  93.         /* get old rect */
  94.         tempRect.left = (*ScrollVHandle)->contrlRect.left;
  95.         tempRect.bottom = (*ScrollVHandle)->contrlRect.bottom;
  96.         temp2Rect.left = MyWindow->portRect.left;
  97.         temp2Rect.top = MyWindow->portRect.top;
  98.         temp2Rect.right = MyWindow->portRect.right;
  99.         temp2Rect.bottom = MyWindow->portRect.bottom;
  100.         Index = temp2Rect.bottom - temp2Rect.top - 13;
  101.         tempRect.top = -1;
  102.         
  103.         /* move control */
  104.         HideControl(ScrollVHandle);
  105.         SizeControl(ScrollVHandle, 16,Index);
  106.         MoveControl(ScrollVHandle,
  107.                 temp2Rect.right - temp2Rect.left-15,tempRect.top);
  108.         ShowControl(ScrollVHandle);
  109.         HUnlock((Handle)ScrollVHandle);
  110.         } /* take care of scrollbar */
  111.     
  112.     SetPort(SavePort);
  113.     } /* MyWindow */
  114. } /* Resized_MyWindow() */
  115.  
  116. /**********************
  117. UpDate_MyWindow() handles updates and printing of MyWindow
  118. ***********************/
  119. void  UpDate_MyWindow(whichWindow)
  120. WindowPtr  whichWindow;
  121. { /* UpDate_MyWindow() */
  122. WindowPtr   SavePort, OldPort;
  123. PicHandle   Pic_Handle;
  124. Str255   sTemp;
  125. Rect tempRect;
  126.     
  127. if ((MyWindow != NIL)  &&  (MyWindow == whichWindow)) { /* MyWindow */
  128.     /* Draw Picture */
  129.     Pic_Handle = GetPicture(PictureID);
  130.     SetRect(&tempRect, 151,100,215,182);
  131.     if (Pic_Handle != NIL)
  132.         DrawPicture ( Pic_Handle , &tempRect);
  133.     
  134.     /* Draw static text */
  135.     TextFont(systemFont);
  136.     SetRect(&tempRect, 9,45,75,62);
  137.     strcpy((char *) &sTemp,"Message:");
  138.     CtoPstr((char *)&sTemp);
  139.     TextBox(&sTemp[1], sTemp[0], &tempRect, teJustLeft);
  140.     TextFont(applFont);
  141.     
  142.     /* Draw or Print TE record */
  143.     SetRect(&tempRect, 8,68,124,90);
  144.     FrameRect(&tempRect);
  145.     InsetRect(&tempRect,3,3);
  146.     if (theTE != NIL)
  147.         if (!forPrinting)
  148.             TEUpdate(&tempRect,theTE); /* normal updating */
  149.         else {
  150.             OldPort = (**theTE).inPort; /* change port in TE */
  151.             GetPort(&SavePort);
  152.             (**theTE).inPort = SavePort;
  153.             TEUpdate(&tempRect,theTE); /* normal updating */
  154.             (**theTE).inPort = OldPort; /* restore port in TE */
  155.             }
  156.     
  157.     /* Draw or Print List */
  158.     SetRect(&tempRect, 4,115,148,179);
  159.     tempRect.right = tempRect.right - 15;
  160.     InsetRect(&tempRect,-1,-1);
  161.     FrameRect(&tempRect);
  162.     if (List_I_AList != NIL)
  163.         if (!forPrinting)
  164.             LUpdate(MyWindow->visRgn,List_I_AList);
  165.         else 
  166.             PrintList(List_I_AList); /* call print list */
  167.     
  168.     /* Draw PopUp Menu */
  169.     TextSize(12);
  170.     TextFont(systemFont);
  171.     TextSize(12);
  172.     MoveTo(86,56);
  173.     DrawString("\pPopUp:");
  174.     SetRect(&tempRect,133,43,197,60);
  175.     EraseRect(&tempRect);
  176.     MoveTo(146,56);
  177.     switch (Popup_I_PopUp) { /* Draw Current PopUp Selection */
  178.         case 1:
  179.             DrawString("\pFirst");
  180.             break;
  181.         case 2:
  182.             DrawString("\pSecond");
  183.             break;
  184.         case 3:
  185.             DrawString("\pThird");
  186.             break;
  187.         default:
  188.             DrawString("\p????");
  189.             break;
  190.         }
  191.     FrameRect(&tempRect);
  192.     MoveTo(tempRect.left + 1, tempRect.bottom);
  193.     LineTo(tempRect.right, tempRect.bottom);
  194.     LineTo(tempRect.right, tempRect.top + 1);
  195.     TextSize(12);
  196.     TextFont(applFont);
  197.     
  198.     /* Draw or Print Controls */
  199.     if (!forPrinting)
  200.         DrawControls(MyWindow);
  201.     else
  202.         PrintControls(((WindowPeek) MyWindow)->controlList);
  203.     DrawGrowIcon(MyWindow);
  204.     } /* MyWindow */
  205. } /* UpDate_MyWindow() */
  206.  
  207. /**********************
  208. HandleScrollBar() takes care of scrollbar
  209. ***********************/
  210. static void  HandleScrollBar(code, Start, Stop, Increment, LIncrement, theControl,myPt)
  211. short   code;
  212. short   Start;
  213. short   Stop;
  214. short   LIncrement,Increment;
  215. ControlHandle   theControl;
  216. Point   myPt;
  217. { /* HandleScrollBar() */
  218. short   theValue;
  219. long   MaxTick;
  220.     
  221. do { /* button pressed */
  222.     HiliteControl(theControl, code);
  223.     theValue = GetCtlValue(theControl);
  224.         
  225.     if (code == inUpButton) {
  226.         theValue = theValue - Increment;
  227.         if (theValue < Start)
  228.             theValue = Start;
  229.         }
  230.     if (code == inDownButton) {
  231.         theValue = theValue + Increment;
  232.         if (theValue > Stop)
  233.             theValue = Stop;
  234.         }
  235.     if (code == inPageUp) {
  236.         theValue = theValue - LIncrement;
  237.         if (theValue < Start)
  238.             theValue = Start;
  239.         }
  240.     if (code == inPageDown) {
  241.         theValue = theValue + LIncrement;
  242.         if (theValue > Stop)
  243.             theValue = Stop;
  244.         }
  245.     if (code == inThumb) {
  246.         code = TrackControl(theControl, myPt, NIL);
  247.         theValue = GetCtlValue(theControl);
  248.         }
  249.         
  250.     SetCtlValue(theControl, theValue);
  251.         
  252.     MaxTick = TickCount() + 9; /* delay */
  253.     do {  } while ( (Button() == TRUE) && (TickCount() <= MaxTick));
  254.     
  255.     HiliteControl(theControl, 0);/* Lighten the arrow */
  256.     } while (StillDown( ) == TRUE); /* button pressed */
  257. } /* HandleScrollBar() */
  258.  
  259. /**********************
  260. Open_MyWindow() opens MyWindow
  261. ***********************/
  262. void Open_MyWindow(theInput)
  263. TEHandle    *theInput;
  264. { /* Open_MyWindow() */
  265. short    Index;
  266. Rect    dataBounds;
  267. Point    cSize;
  268. short    theRow;
  269. Rect tempRect, temp2Rect;
  270. Str255 sTemp;
  271.  
  272. if (MyWindow == NIL) {
  273.     MyWindow = GetNewWindow(1,NIL, (WindowPtr)-1);
  274.     SetPort(MyWindow);
  275.     
  276.     /* Get Controls */
  277.     C_A_Button = GetNewControl(I_A_Button,MyWindow);
  278.     C_A_Checkbox = GetNewControl(I_A_Checkbox,MyWindow);
  279.     R1Control[1] = GetNewControl ( I_Radio_1 , MyWindow );
  280.     R1Control[2] = GetNewControl ( I_Radio_2 , MyWindow );
  281.     R1Control[3] = GetNewControl ( I_Radio_3 , MyWindow );
  282.     
  283.     /* get scrollbar */
  284.     SetRect(&tempRect,310,-1,326,195);
  285.     temp2Rect.left = MyWindow->portRect.left;
  286.     temp2Rect.right = MyWindow->portRect.right;
  287.     temp2Rect.top = MyWindow->portRect.top;
  288.     temp2Rect.bottom = MyWindow->portRect.bottom;
  289.     ScrollVHandle = GetNewControl(I_Scroll_bar,MyWindow);
  290.     Index = temp2Rect.bottom - temp2Rect.top - 13;
  291.     tempRect.top = 0;
  292.     SizeControl(ScrollVHandle, 16,Index);
  293.     MoveControl(ScrollVHandle, temp2Rect.right - temp2Rect.left-15,tempRect.top-1);
  294.     
  295.     /* get TE record */
  296.     SetRect(&tempRect, 8,68,124,90);
  297.     FrameRect(&tempRect);
  298.     InsetRect(&tempRect,3,3);
  299.     theTE= TENew(&tempRect,&tempRect);
  300.     if (*theInput != NIL) 
  301.         TEDeactivate(*theInput); 
  302.     *theInput = theTE; 
  303.     HLock((Handle)theTE);
  304.     (*theTE)->txFont = systemFont;
  305.     (*theTE)->fontAscent = 12;
  306.     (*theTE)->lineHeight = 12 + 3 + 1;
  307.     HUnlock((Handle)theTE);
  308.     strcpy((char *)&sTemp,"Hello there!");
  309.     CtoPstr((char *)&sTemp);
  310.     TESetText(&sTemp[1],sTemp[0],theTE);
  311.     TEActivate(theTE);
  312.     TEUpdate(&tempRect,theTE);
  313.     TextFont(applFont);
  314.     
  315.     /* get list */
  316.     SetRect(&tempRect, 4,115,148,179);
  317.     Rect_I_AList = tempRect;
  318.     tempRect.right = tempRect.right - 15;
  319.     InsetRect(&tempRect,-1,-1);
  320.     FrameRect(&tempRect);
  321.     InsetRect(&tempRect,1,1);
  322.     SetRect(&dataBounds,0,0,1,0);
  323.     cSize.h = tempRect.right - tempRect.left;
  324.     cSize.v = 0;
  325.     List_I_AList = LNew(&tempRect,&dataBounds,cSize,0,MyWindow,TRUE,FALSE,FALSE,TRUE);
  326.     (*List_I_AList)->selFlags = lOnlyOne + lNoNilHilite;
  327.     LDoDraw(TRUE,List_I_AList);
  328.     
  329.     /* add items to list */
  330.     cSize.h = 0;
  331.     strcpy((char *) &sTemp,"\pFirst Item");
  332.     theRow = LAddRow(1, 200, List_I_AList);
  333.     cSize.v = theRow;
  334.     LSetCell(&sTemp[1], sTemp[0], cSize, List_I_AList);
  335.     LDraw(cSize,  List_I_AList);
  336.     
  337.     strcpy((char *)&sTemp,"\pSecond Item");
  338.     theRow = LAddRow(1, 200, List_I_AList);
  339.     cSize.v = theRow;
  340.     LSetCell(&sTemp[1], sTemp[0], cSize, List_I_AList);
  341.     LDraw(cSize,  List_I_AList);
  342.     
  343.     strcpy((char *)&sTemp,"\pThird Item");
  344.     theRow = LAddRow(1, 200, List_I_AList);
  345.     cSize.v = theRow;
  346.     LSetCell(&sTemp[1], sTemp[0], cSize, List_I_AList);
  347.     LDraw(cSize,  List_I_AList);
  348.     
  349.     strcpy((char *)&sTemp,"\pFourth Item");
  350.     theRow = LAddRow(1, 200, List_I_AList);
  351.     cSize.v = theRow;
  352.     LSetCell(&sTemp[1], sTemp[0], cSize, List_I_AList);
  353.     LDraw(cSize,  List_I_AList);
  354.     
  355.     strcpy((char *)&sTemp,"\pFifth Item");
  356.     theRow = LAddRow(1, 200, List_I_AList);
  357.     cSize.v = theRow;
  358.     LSetCell(&sTemp[1], sTemp[0], cSize, List_I_AList);
  359.     LDraw(cSize,  List_I_AList);
  360.     
  361.     cSize.h = 0;
  362.     cSize.v = 0;
  363.     LSetSelect(TRUE,cSize,List_I_AList);
  364.     TextSize(12);
  365.     
  366.     /* set up PopUp menu */    
  367.     Popup_I_PopUp = 1;
  368.     ShowWindow(MyWindow);
  369.     SelectWindow(MyWindow);
  370.     }
  371.     else
  372.         SelectWindow(MyWindow);
  373. } /* Open_MyWindow() */
  374.  
  375. /**********************
  376. Do_A_Checkbox() Handles checkbox and radios
  377. ***********************/
  378. static  void  Do_A_Checkbox(theControl)
  379. ControlHandle    theControl;
  380. { /* Do_A_Checkbox() */
  381. short    RefCon;
  382. short    theValue, Index;
  383.  
  384. RefCon = GetCRefCon(theControl);
  385. theValue = GetCtlValue(theControl);
  386. theValue = (theValue + 1) & 1;
  387.     
  388. switch (RefCon) { /* switch on type */
  389.     case I_A_Checkbox:
  390.         SetCtlValue(theControl, theValue);
  391.         break;
  392.     
  393.     case I_Radio_1:
  394.     case I_Radio_2:
  395.     case I_Radio_3:
  396.         for (Index = 1; Index < 4; Index++) /* clear radios first */
  397.             SetCtlValue(R1Control[Index],0);
  398.         SetCtlValue(theControl, 1);
  399.         break;
  400.     default:
  401.         break;
  402.     } /* switch on type */
  403. } /* Do_A_Checkbox() */
  404.  
  405. /**********************
  406. Do_MyWindow()
  407. ***********************/
  408. void  Do_MyWindow(myEvent, theInput)
  409. EventRecord    *myEvent;
  410. TEHandle    *theInput;
  411. { /* Do_MyWindow() */
  412. short    code;
  413. WindowPtr    whichWindow;
  414. Point    myPt;
  415. ControlHandle    theControl;
  416. char    DoubleClick;
  417. long theCell;
  418. Rect tempRect;
  419. MenuHandle    tempMenu;
  420. long    Result;
  421.  
  422. if (MyWindow != NIL) { /* MyWindow */
  423.     code = FindWindow(myEvent->where, &whichWindow);
  424.     
  425.     if ((myEvent->what == mouseDown)  &&  (MyWindow == whichWindow)) { /* mouse event */                                 
  426.         myPt = myEvent->where;
  427.         GlobalToLocal(&myPt);
  428.          
  429.         SetRect(&tempRect, 8,68,124,90);
  430.         if (PtInRect(myPt,&tempRect) == TRUE) { /* handle TE record */                             
  431.             if (*theInput != NIL)       
  432.                 TEDeactivate(*theInput);
  433.             *theInput = theTE;        
  434.             TEActivate(*theInput);      
  435.             TEClick(myPt, FALSE, theTE);
  436.             } /* handle TE record */                        
  437.          
  438.         if (PtInRect(myPt,&Rect_I_AList) == TRUE) { /* handle list */
  439.             DoubleClick = LClick(myPt, myEvent->modifiers, List_I_AList);
  440.             theCell = LLastClick(List_I_AList);
  441.             } /* handle list */
  442.          
  443.         SetRect(&tempRect, 81,42,204,62);
  444.         if ( PtInRect ( myPt , &tempRect ) == TRUE ) { /* in PopUp */
  445.             ChangedSelection = FALSE;
  446.             tempMenu = GetMenu(PopUpID);
  447.             if (tempMenu != NIL) { /* Good Menu */
  448.                 
  449.                 /* Select Menu Item */
  450.                 InsertMenu (tempMenu,-1);
  451.                 SetRect(&tempRect,81,44,133,60);
  452.                 InvertRect(&tempRect);
  453.                 myPt.v = 44;
  454.                 myPt.h = 133;
  455.                 LocalToGlobal(&myPt);
  456.                 CheckItem(tempMenu,Popup_I_PopUp,TRUE);
  457.                 Result = PopUpMenuSelect(tempMenu, myPt.v, myPt.h,  Popup_I_PopUp);
  458.                 if ((Popup_I_PopUp!= LoWord(Result)) && (LoWord(Result) > 0)) {                       
  459.                     CheckItem(tempMenu,Popup_I_PopUp,FALSE);
  460.                     ChangedSelection = TRUE;
  461.                     Popup_I_PopUp= LoWord(Result);
  462.                     }                       
  463.                 else                    
  464.                     CheckItem(tempMenu,Popup_I_PopUp,FALSE);
  465.                 DeleteMenu(PopUpID);
  466.                 
  467.                 /* clean up */
  468.                 InvertRect(&tempRect);
  469.                 if (ChangedSelection == TRUE) { /* show new selection */                      
  470.                     SetRect(&tempRect,134,44,196,59);
  471.                     EraseRect(&tempRect);
  472.                     MoveTo(tempRect.left + 12,tempRect.top + 12);
  473.                     TextFont(systemFont);
  474.                     switch (Popup_I_PopUp) {
  475.                         case 1:         
  476.                             DrawString("\pFirst");
  477.                             break;
  478.                         case 2:         
  479.                             DrawString("\pSecond");
  480.                             break;
  481.                         case 3:         
  482.                             DrawString("\pThird");
  483.                             break;
  484.                         default:
  485.                             break;
  486.                         }
  487.                     TextFont(applFont);
  488.                     } /* show new selection */
  489.                 } /* Good Menu */
  490.             whichWindow = NIL;
  491.             } /* in PopUp */
  492.         
  493.          } /* mouse event */
  494.      
  495.     if ((MyWindow == whichWindow) &&  (code == inContent)) { /* handle controls */
  496.         code = FindControl(myPt, whichWindow, &theControl);
  497.         
  498.         if ((code == inUpButton) || (code == inDownButton) 
  499.                 || (code == inThumb) ||  (code == inPageDown) || (code == inPageUp))
  500.             HandleScrollBar(code,1,100,1,10,theControl,myPt);
  501.         if (code != 0)
  502.             code = TrackControl(theControl,myPt, NIL);
  503.         if (code == inCheckBox)
  504.             Do_A_Checkbox(theControl);
  505.         } /* handle controls */
  506.     } /* MyWindow */
  507. } /* Do_MyWindow() */
  508.